//
//  FeatureViewController.swift
//  QNN
//
//  Created by zhenyu on 17/3/30.
//  Copyright © 2017年 qianshengqian. All rights reserved.
//

import UIKit
import FSPagerView

class FeatureViewController: UIViewController {
  
  weak var pageControl: FSPageControl!
  weak var btn: UIButton!
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    // Do any additional setup after loading the view.
    let scrollView = UIScrollView()
    scrollView.showsHorizontalScrollIndicator = false
    scrollView.bounces = false
    scrollView.isPagingEnabled = true
    scrollView.frame = view.bounds
    scrollView.delegate = self
    view.addSubview(scrollView)
    
    let imagePrefix = iPhone4 ? "small_" : "big_"
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapAction))
    for i in 1...3 {
      let imageName = "\(imagePrefix)\(i)"
      let imgView = UIImageView(image: UIImage(named: imageName))
      let x = view.bounds.width * CGFloat(i-1)
      imgView.frame = CGRect(x: x, y: 0, width: view.bounds.width, height: view.bounds.height)
      scrollView.addSubview(imgView)
      
      if i == 3 {
        imgView.isUserInteractionEnabled = true
        imgView.addGestureRecognizer(tapGesture)
        
      }
    }
    scrollView.contentSize = CGSize(width: view.bounds.width * 3, height: 0)
    
    setVeiws()
  }
  
  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
  
  deinit {
    debugPrint("FeatureViewController deinit")
  }
  
  // MARK: - setView
  func setVeiws() {
    pageControl = {
      let p = FSPageControl()
      p.setFillColor(UIColor(hexString: "#DB3738"), for: .selected)
      p.setStrokeColor(UIColor(hexString: "#FFCBCC"), for: .normal)
      p.contentHorizontalAlignment = .center
      p.numberOfPages = 3
      view.addSubview(p)
      p.snp.makeConstraints { (make) in
        make.bottom.equalTo(view.snp.bottom).offset(-25)
        make.left.right.equalTo(view)
        make.height.equalTo(25)
      }
      return p
    }()
    
    btn = {
      let b = UIButton()
      b.layer.cornerRadius = 5
      b.layer.borderColor = UIColor(hexString: "59B159").cgColor
      b.layer.borderWidth = 0.5
      b.isHidden = true
      b.isUserInteractionEnabled = false
      b.setTitle("立即体验", for: .normal)
      b.setTitleColor(UIColor(hexString: "59B159"), for: .normal)
      b.titleLabel?.font = HRFontt(size: 13, bold: true)
      view.addSubview(b)
      b.snp.makeConstraints { (make) in
        make.size.equalTo(CGSize(width: 120, height: 36))
        make.centerX.equalTo(view)
        make.bottom.equalTo(pageControl.snp.top).offset(-3)
      }
      return b
    }()
  }
  
  // MARK: - Action
  func tapAction() {
    debugPrint("最后一张")
    window?.rootViewController = TabBarController()
  }
}
  

// MARK: - UIScrollViewDelegate
extension FeatureViewController: UIScrollViewDelegate {
  func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offset = scrollView.contentOffset.x
    let index = offset/ScreenWidth
    pageControl.currentPage = Int(index)
    btn.isHidden = index == 2 ? false : true
  }
}
